Conditions | 1 |
Paths | 64 |
Total Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* globals numberformat,window */ |
||
14 | function($sce, data) { |
||
15 | let sv = this; |
||
16 | /* Return the HTML representation of an element, or the element itself |
||
17 | if it doesn't have one */ |
||
18 | sv.getHTML = function(resource) { |
||
19 | let html = data.html[resource]; |
||
20 | if (typeof html === 'undefined' && data.resources[resource]) { |
||
21 | html = data.resources[resource].html; |
||
22 | } |
||
23 | if (typeof html === 'undefined') { |
||
24 | return resource; |
||
25 | } |
||
26 | return html; |
||
27 | }; |
||
28 | |||
29 | sv.prettifyNumber = function(number, player) { |
||
30 | if (typeof number === 'undefined' || number === null) return null; |
||
|
|||
31 | if (number === '') return ''; |
||
32 | if (number === Infinity) return '∞'; |
||
33 | if (number === 0) return '0'; |
||
34 | return numberformat.format(number, player.options.numberformat); |
||
35 | }; |
||
36 | |||
37 | sv.addResource = function(player, scope, key, quantity, state){ |
||
38 | player.resources[key].number += quantity; |
||
39 | sv.addStatistic(player, scope, key, quantity); |
||
40 | if (quantity > 0 && !player.resources[key].unlocked) { |
||
41 | player.resources[key].unlocked = true; |
||
42 | state.addNew(key); |
||
43 | } |
||
44 | }; |
||
45 | |||
46 | /* Adds an statistic. Scope can be only all time, dark run, |
||
47 | only for specific elements, or for all elements at once |
||
48 | */ |
||
49 | sv.addStatistic = function(player, scope, key, value){ |
||
50 | setStatistic(player.statistics.all_time, key, value); |
||
51 | if(scope === 'all_time'){ |
||
52 | return; |
||
53 | } |
||
54 | setStatistic(player.statistics.dark_run, key, value); |
||
55 | if(scope === 'dark'){ |
||
56 | return; |
||
57 | } |
||
58 | if(scope === 'all_elements') { |
||
59 | scope = Object.keys(data.elements); |
||
60 | } |
||
61 | for(let element of scope){ |
||
62 | player.statistics.exotic_run[element] = player.statistics.exotic_run[element] || {}; |
||
63 | setStatistic(player.statistics.exotic_run[element], key, value); |
||
64 | } |
||
65 | }; |
||
66 | |||
67 | function setStatistic(bucket, key, value){ |
||
68 | // If it is numeric, add it up |
||
69 | if(!isNaN(parseFloat(value)) && isFinite(value)){ |
||
70 | bucket[key] = bucket[key]+value || value; |
||
71 | // otherwise, replace |
||
72 | }else{ |
||
73 | bucket[key] = value; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | sv.trustHTML = function(html) { |
||
78 | return $sce.trustAsHtml(html); |
||
79 | }; |
||
80 | |||
81 | sv.nextAmount = function (player, index, array) { |
||
82 | player.options[index] = (player.options[index] + 1) % array.length; |
||
83 | }; |
||
84 | |||
85 | sv.delayedExec = function(currentTs, eventTs, delay, callback) { |
||
86 | if(currentTs-eventTs >= delay){ |
||
87 | callback(); |
||
88 | }else{ |
||
89 | window.requestAnimationFrame((ts) => sv.delayedExec(ts, eventTs, delay, callback)); |
||
90 | } |
||
91 | }; |
||
92 | |||
93 | sv.prestigeProduction = function(number, start, power){ |
||
94 | number = number || 0; |
||
95 | let production = Math.pow(Math.E,(-0.5+Math.sqrt(0.25+0.8686*Math.log(number/start)))/(2/Math.log(power*power))) || 0; |
||
96 | return Math.round(Math.max(0, production)); |
||
97 | }; |
||
98 | |||
99 | sv.calculateValue = function(number, value, level){ |
||
100 | let result = number; |
||
101 | if(value.linear) result *= level*value.linear; |
||
102 | if(value.poly) result *= Math.floor(Math.pow(level, value.poly)); |
||
103 | if(value.exp) result *= Math.floor(Math.pow(value.exp, level)); |
||
104 | return result; |
||
105 | } |
||
106 | } |
||
107 | ]); |
||
108 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.